package com.zou.application.controller.customer;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.zou.application.dao.UserEntityMapper;
import com.zou.application.entity.UserEntity;
import com.zou.application.utils.RandomUtils;
import com.zou.application.utils.StringUtils;

@RequestMapping("/user")
@Controller
public class UserController {
	
	@Autowired
	private UserEntityMapper userEntityMapper;
	
	@RequestMapping("/login")
	public String loginPage(){
		return "/customer/login";
	}
	
	@RequestMapping("/register")
	public String register(HttpServletRequest request){
		request.setAttribute("code", "200");
		request.setAttribute("msg", "");
		return "/customer/register";
	}
	
	/**
	 * 客户注册/注册成功到个人中心
	 * @param request
	 * @return
	 */
	@RequestMapping("/customerRegister")
	public ModelAndView customerRegister(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/customer/register");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String email = request.getParameter("email");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		if(StringUtils.isEmpty(username)){
			mv.addObject("code","100");
			mv.addObject("message","用户名不能为空");
		}else if(StringUtils.isEmpty(password)){
			mv.addObject("code","100");
			mv.addObject("message","用户名不能为空");
		}
		UserEntity user = userEntityMapper.getByUserName(username);
		if(user != null){
			mv.addObject("code","100");
			mv.addObject("msg","用户名已存在");
		}else{
			UserEntity userEntity = new UserEntity();
			userEntity.setUserName(username);
			userEntity.setCreater(username);
			userEntity.setCreateTime(new Date());
			userEntity.setEmail(email);
			userEntity.setIsDelete((short)0);
			userEntity.setPassword(password);
			userEntity.setSex((short)1);
			userEntity.setName(username);
			userEntity.setAddress(address);
			userEntity.setPhone(phone);
			userEntity.setStatus((short)1);
			userEntity.setUserCode(RandomUtils.randomString(4)
					+RandomUtils.randomNumber(6));
			userEntityMapper.insert(userEntity);
			mv.addObject("code","200");
			mv.addObject("msg","注册成功");
			mv.addObject("data",username);
			mv.setViewName("/customer/login");
		}
		return mv;
	}
	
	@RequestMapping("/shopcar")
	public ModelAndView shopcar(HttpServletRequest request){
		ModelAndView mv = new ModelAndView("/customer/login");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		
		if(StringUtils.isEmpty(username)){
			mv.addObject("code","100");
			mv.addObject("message","用户名不能为空");
		}else if(StringUtils.isEmpty(password)){
			mv.addObject("code","100");
			mv.addObject("message","用户名不能为空");
		}
		UserEntity user = userEntityMapper.getByUserName(username);
		if(user != null){
			mv.addObject("code","100");
			mv.addObject("msg","密码错误");
			if(user.getPassword().equals(password)){
				mv.addObject("code","100");
				mv.addObject("data",username);
				mv.setViewName("/customer/shopcar");
			}
		}else{
			mv.addObject("code","100");
			mv.addObject("msg","用户不存在");
		}
		return mv;
	}
	
	@RequestMapping("/customerLogin")
	public ModelAndView customerLogin(HttpServletRequest request,HttpSession session){
		ModelAndView mv = new ModelAndView("/customer/login");
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if(StringUtils.isEmpty(username)){
			mv.addObject("code","100");
			mv.addObject("message","用户名不能为空");
		}else if(StringUtils.isEmpty(password)){
			mv.addObject("code","100");
			mv.addObject("message","用户名不能为空");
		}
		UserEntity user = userEntityMapper.getByUserName(username);
		session.setAttribute("userId", user.getId());
		if(user != null){
			mv.addObject("code","100");
			mv.addObject("msg","密码错误");
			if(user.getPassword().equals(password)){
				mv.addObject("code","100");
				mv.addObject("user",user);
				mv.addObject("contextPath",request.getContextPath());
				mv.setViewName("/customer/myinfo");
			}
		}
		return mv;
	}
	
	@RequestMapping("/usercenter")
	public ModelAndView usercenter(HttpServletRequest request,HttpSession session){
		ModelAndView mv = new ModelAndView("/customer/login");
		Integer userId = (Integer) session.getAttribute("userId");
		if(userId == null){
			mv.setViewName("/customer/login");
			return mv;
		}
		UserEntity user = userEntityMapper.selectById(userId);
		if(user != null){
			mv.addObject("code","100");
			mv.addObject("user",user);
			mv.addObject("contextPath",request.getContextPath());
			mv.setViewName("/customer/myinfo");
		}
		return mv;
	}
	
	@RequestMapping("/myinfo")
	public ModelAndView myinfo(HttpServletRequest request,HttpSession session){
		ModelAndView mv = new ModelAndView();
		Integer userId = (Integer) session.getAttribute("userId");
		if(userId == null){
			mv.addObject("code","100");
			mv.addObject("message","用户名不能为空");
		}
		UserEntity user = userEntityMapper.selectById(userId);
		if(user != null){
			mv.addObject("code","100");
			mv.addObject("user",user);
			mv.addObject("contextPath",request.getScheme()+"://"+request.getRemoteAddr()+request.getRemotePort());
			System.out.println(request.getScheme()+"://"+request.getRemoteAddr()+request.getRemotePort());
			mv.setViewName("/customer/myinfo");
		}else{
			mv.addObject("code","100");
			mv.addObject("msg","用户不存在");
		}
		return mv;
	}
	
	@RequestMapping("/changePasswordPage")
	public String changePasswordPage(){
		return "/customer/change_password";
	}
	
	@RequestMapping("/changePassword")
	@ResponseBody
	public JSONObject changePassword(HttpSession session,HttpServletRequest request){
		JSONObject result = new JSONObject();
		Integer userId = (Integer) session.getAttribute("userId");
		if(userId == null){
			result.put("code","100");
			result.put("msg","用户名不能为空");
		}
		try {
			String oldPassword = request.getParameter("oldPassword");
			String newPassword = request.getParameter("newPassword");
			String againPassword = request.getParameter("againPassword");
			if(StringUtils.isEmpty(oldPassword)){
				result.put("code","100");
				result.put("msg","原始密码不能为空");
				return result;
			}else if(StringUtils.isEmpty(newPassword)){
				result.put("code","100");
				result.put("msg","新密码不能为空");
				return result;
			}else if(StringUtils.isEmpty(againPassword)){
				result.put("code","100");
				result.put("msg","重复密码不能为空");
				return result;
			}else if(!newPassword.equals(againPassword)){
				result.put("code","100");
				result.put("msg","两次密码不一致");
				return result;
			}
			UserEntity userEntity = userEntityMapper.selectById(userId);
			if(oldPassword.equals(userEntity.getPassword())){
				UserEntity item = new UserEntity();
				item.setPassword(newPassword);
			}else{
				result.put("code","100");
				result.put("msg","密码不正确");
				return result;
			}
			result.put("code", "200");
			result.put("msg", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", "100");
			result.put("msg", "密码修改失败");
		}
		return result;
	}
	
	@RequestMapping("/checkUserRegis")
	@ResponseBody
	public JSONObject checkUserRegis(HttpSession session,HttpServletRequest request){
		JSONObject result = new JSONObject();
		try {
			String username = request.getParameter("username");
			if(StringUtils.isEmpty(username)){
				result.put("code","100");
				result.put("msg","参数错误");
				return result;
			}
			UserEntity userEntity = userEntityMapper.getByUserName(username);
			if(userEntity!=null){
				result.put("code","100");
				result.put("msg","用户名已注册");
				return result;
			}
			result.put("code", "200");
			result.put("msg", "成功");
		} catch (Exception e) {
			e.printStackTrace();
			result.put("code", "100");
			result.put("msg", "系统异常");
		}
		return result;
	}
}
